package managedbean;

import java.util.Map;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import jpa.CategoryJPA;
import jpa.PetJPA;
import jpa.ShoppingCart;
import jpa.StatusJPA;
import ejb.CatalogFacadeRemote;


/**
 * Managed Bean AddPetToShoppingCartMBean
 */
@ManagedBean(name = "addPet")
@SessionScoped
public class AddPetToShoppingCartMBean {
	private static final long serialVersionUID = 1L;

	@EJB
	private CatalogFacadeRemote addPetToCartRemote;
	protected PetJPA pet;
	
	public void addPetToShoppingCart() throws Exception
	{	
		pet = new PetJPA();
		FacesContext fc = FacesContext.getCurrentInstance();
		getPetFromParams(fc);
		ShoppingCart cart = (ShoppingCart)fc.getExternalContext().getSessionMap().get("shoppingCart");
		cart.getPets().add(pet);
	}

	public PetJPA getPet() {
		return pet;
	}

	public void setPet(PetJPA pet) {
		this.pet = pet;
	}
	
	private void getPetFromParams(FacesContext fc) {
		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
		CategoryJPA category = new CategoryJPA();
		category.setName(params.get("category"));
		pet.setCategory(category);
		pet.setId(Integer.parseInt(params.get("id")));
		pet.setName(params.get("name"));
		pet.setDatebirth(params.get("birth"));
		pet.setPrice(Float.parseFloat(params.get("price")));
		pet.setDescription(params.get("description"));
		StatusJPA status = new StatusJPA();
		status.setStatus(params.get("status"));
		pet.setStatus(status);
		pet.setPhoto(params.get("photo"));
	}
}
